package com.hongshan.wallet.core.home

import android.arch.lifecycle.Observer
import com.hongshan.wallet.BR
import com.hongshan.wallet.Constant
import com.hongshan.wallet.MyApplication
import com.hongshan.wallet.R
import com.hongshan.wallet.base.BaseRecyclerAdapter
import com.hongshan.wallet.base.BaseViewHolder
import com.hongshan.wallet.base.MyActivity
import com.hongshan.wallet.base.MyViewModel
import com.hongshan.wallet.bean.PaymentMethod
import com.hongshan.wallet.router.RouterIndex
import com.hongshan.wallet.router.go
import com.hongshan.wallet.toast

/**
 * Copyright Zhangxx.
 * Copyright (c) 2019
 * All rights reserved
 *
 * @project  : Wallet
 * @package  : com.example.wallet.core.home
 * @author   : zhangxx
 * @date     : 2019/4/10
 * @email    : qq957413212@hotmail.com
 * @desc     : //TODO
 */
class PayMethodAdapter(val payMethods: List<PaymentMethod>, val act: MyActivity<*, *>) : BaseRecyclerAdapter() {

    val bankCard: MutableList<PaymentMethod>
    var wechat: PaymentMethod? = null
    var alipay: PaymentMethod? = null
    val bankVms: MutableList<PayMethodRecycleViewModel> = arrayListOf()
    val WxVm = PayMethodRecycleViewModel(MyApplication.applicationContext)
    val aliVm = PayMethodRecycleViewModel(MyApplication.applicationContext)
    val bankTitle = PayMethodRecycleViewModel(MyApplication.applicationContext)
    val otherTitle = PayMethodRecycleViewModel(MyApplication.applicationContext)
    val topUp = PayMethodRecycleViewModel(MyApplication.applicationContext)

    private val uncheck = Observer<Int> { position ->
        val vm = getVm(position!!)
        arrayListOf<PayMethodRecycleViewModel>().apply {
            addAll(bankVms)
            add(aliVm)
            add(WxVm)
        }.filter { it != vm }.forEach { it.checked.set(false) }
    }

    val next = Observer<Unit> {
        val list = arrayListOf<PayMethodRecycleViewModel>().apply {
            addAll(bankVms)
            add(aliVm)
            add(WxVm)

        }.filter { it.checked.get() == true }
        if (list.isNullOrEmpty()) {
            toast("请选择一种支付方式")
        } else {
            go(RouterIndex.TOP_UP, mapOf(Constant.ROUTE_PARAMS_KEY.TOP_UP_PAY_METHOD to list[0].payMethod.get()))
        }
    }

    init {
        bankCard = arrayListOf()
        payMethods.forEach {
            when (it.code) {
                PaymentMethod.CODE_WECHAT -> {
                    wechat = it
                    WxVm.payMethod.set(wechat)
                    WxVm.otherUncheck.observe(act, uncheck)
                }
                PaymentMethod.CODE_ALIPAY -> {
                    alipay = it
                    aliVm.payMethod.set(alipay)
                    aliVm.otherUncheck.observe(act, uncheck)
                }
                else -> {
                    bankCard.add(it)
                    val vm = PayMethodRecycleViewModel(MyApplication.applicationContext)
                    vm.payMethod.set(it)
                    vm.otherUncheck.observe(act, uncheck)
                    bankVms.add(vm)
                }
            }
        }
        topUp.next.observe(act,next)
    }

    override fun getViewModel(holder: BaseViewHolder<MyViewModel>, position: Int): PayMethodRecycleViewModel {
        val vm = getVm(position)
        vm.position = position
        return vm
    }

    private fun getVm(position: Int) = when (position) {
        0 -> bankTitle
//        payMethods.size - 1 -> otherTitle
//        payMethods.size -> WxVm
//        payMethods.size + 1 -> aliVm
//        payMethods.size + 2 -> topUp
        bankCard.size + 1 -> topUp
        else -> bankVms[position - 1]
    }

    override fun getLayoutIdForPosition(position: Int) = when (position) {
        0 -> R.layout.list_item_top_up_into_bank_card
//        payMethods.size - 1 -> R.layout.list_item_top_up_other_method
//        in payMethods.size..payMethods.size + 1 -> R.layout.list_item_pay_method_alipay_or_wechat
//        payMethods.size + 2 -> R.layout.footer_list_top_up
        payMethods.size + 1 -> R.layout.footer_list_top_up
        else -> R.layout.list_item_pay_method_bank
    }

    override fun initVariableId(position: Int) = BR.vm

    //    override fun getItemCount() = payMethods.size + 3
    override fun getItemCount() = bankCard.size + 2
}